#include "script/glue/LuaGlue.h"
#include <OgreCamera.h>

#include "App.h"

namespace LuaGlue
{
	int Camera::tostring (lua_State *L)
	{
		std::stringstream ss;
		switch(lua_gettop(L))
			{
			case 0:
				lua_pushstring(L, className);
				break;
			case 1:
				lua_pop(L,1);
//				ss << *unwrap<Ogre::Camera>(L, 1);
//				lua_pushstring(L, ss.str().c_str());
				lua_pushstring(L, className);
				break;
			default:
				return 0;
				break;
			}
		return 1;
	}
	
	int Camera::gc(lua_State *L)
	{
		//!TODO
		return 0;
	}

	int Camera::create(lua_State *L)
	{
		const char* name = luaL_checkstring(L, 2);
		Ogre::Camera *cam = new Ogre::Camera(name, ::App::getSingletonPtr()->getSceneManager());
		Wrapper<Camera>::Instance(L, (void*)cam);
		return 1;
	}

	int Camera::getPosition(lua_State *L)
	{
		Ogre::Camera* cam  = unwrap<Ogre::Camera>(L, 1);
//		Ogre::Vector3* v = new Ogre::Vector3(cam->getPosition());
		Ogre::Vector3* v = new Ogre::Vector3(cam->getDerivedPosition());
		Wrapper<Vector3>::Instance(L, (void*)v);
		return 1;
	}

	int Camera::setPosition(lua_State *L)
	{
		Ogre::Camera* cam = unwrap<Ogre::Camera>(L, 1);
		Ogre::Vector3* v = unwrap<Ogre::Vector3>(L, 2);
		cam->setPosition(*v);
		return 1;
	}
	
	int Camera::getOrientation(lua_State *L)
	{
		Ogre::Camera* cam  = unwrap<Ogre::Camera>(L, 1);
		Ogre::Quaternion* q = new Ogre::Quaternion(cam->getDerivedOrientation());
		Wrapper<Quaternion>::Instance(L, (void*)q);
		return 1;
	}	

	int Camera::setOrientation(lua_State *L)
	{
		Ogre::Camera* cam = unwrap<Ogre::Camera>(L, 1);
		Ogre::Quaternion* q = unwrap<Ogre::Quaternion>(L, 2);
		cam->setOrientation(*q);
		return 1;
	}

}

DECLARE(Camera)
META(Camera)
{"__tostring", LuaGlue::Camera::tostring},
{"__gc", 	   LuaGlue::Camera::gc},
METHODS(Camera)
method(LuaGlue::Camera, tostring),
method(LuaGlue::Camera, setPosition),
method(LuaGlue::Camera, getPosition),
method(LuaGlue::Camera, getOrientation),
method(LuaGlue::Camera, setOrientation),
END
